<?php

namespace app\api\controller;

use think\Db;

/**简即美回调**/
class Notify
{
    /**
     * 小程序合伙人缴费回调
     */
    public function xcxJiaoFeiNotify()
    {
        // 启动事务
        Db::startTrans();
        try{
            $data = input();
            write_log($data);
            $out_trade_no = $data['out_trade_no'];//回调返回来的订单号
            $cardPay = db('card_pay')->where('ordernum',$out_trade_no)->find();
            if ($cardPay['ispay'] == 1) {
                Db::rollback();
                exit('failed');
            }
            $firstPrice = $cardPay['price'];
            $user_id = $cardPay['user_id'];
            
            //更新余额
            $user = db('user')->where('id',$user_id)->lock(true)->find();
            
            db('user')->where('id',$user_id)->setInc('storage_balance',$cardPay['price']);
            db('user')->where('id',$user_id)->setInc('first_storage_balance',$cardPay['price']);

            //更新首充状态
            db('user')->where('id',$user_id)->update([
                'is_card_pay'=>1,
                'card_pay_time'=>time()
            ]);
            //增加资金变动明细
            db('card_log')->insert([
                'type'=>3,
                'user_id'=>$user_id,
                'price'=>$firstPrice,
                'new_price'=>$user['storage_balance']+$firstPrice,
                'pay_type'=>1,
                'ordernum'=>generateOrderNumber(),
                'is_first'=>1,
                'ispay'=>1,
                'addtime'=>time(),
                'user_del'=>0
            ]);

            db("card_pay")->where('ordernum',$out_trade_no)->update([
                'ispay'=>1,
                'transaction_id'=>$data['trade_no']
            ]);
                
            
        } catch (\Exception $e) {
        Db::rollback();
            exit('failed');
        }
        exit('success');
    }


    /**
     * 小程序合伙人充值储值
     * @param string $order_no 商户订单号
     * @param string $transaction_id 微信订单号
     */
    public function chongZhiNotify()
    {
        // 启动事务
        Db::startTrans();
        try{
            $data = input();
            write_log($data);
   
            $out_trade_no = $data['out_trade_no'];//回调的订单号
            $cardPay = db('card_pay')->where('ordernum',$out_trade_no)->find();
            if ($cardPay['ispay'] == 1) {
                Db::rollback();
                exit('failed');
            }
            $firstPrice = $cardPay['price'];
            $user_id = $cardPay['user_id'];
        
            //更新余额
            $user = db('user')->where('id',$user_id)->lock(true)->find();
            db('user')->where('id',$user_id)->setInc('storage_balance',$cardPay['price']);

            //增加资金变动明细
            db('card_log')->insert([
                'type'=>3,
                'user_id'=>$user_id,
                'price'=>$firstPrice,
                'new_price'=>$user['storage_balance']+$firstPrice,
                'pay_type'=>1,
                'ordernum'=>generateOrderNumber(),
                'is_first'=>$cardPay['is_first'],
                'ispay'=>1,
                'addtime'=>time(),
                'user_del'=>0
            ]);

            db("card_pay")->where('ordernum',$out_trade_no)->update([
                'ispay'=>1,
                'transaction_id'=>$data['trade_no']
            ]);
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            //$this->error($e->getMessage());
            exit('failed');
        }
        exit('success');
    }

}